package es.us.lsi.daw.soportePropiedades;

import java.beans.PropertyEditorSupport;

import es.us.lsi.daw.dao.DAOComun;
import es.us.lsi.daw.model.util.BaseEntity;
import es.us.lsi.daw.util.Util;


public class ComunEditor extends PropertyEditorSupport{

	private final DAOComun application;
	
	private Class<?> clase;
	
	public <T> ComunEditor(Class<T> clase, DAOComun application) {
		this.application = application;
		this.clase = clase;		
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		setValue(application.obtenerObjetoId(clase, Long.parseLong(text)));		
	}
	
	@Override
	public String getAsText() {
		if(this.getValue() != null && Util.tieneClase(this.getValue(),BaseEntity.class) && ((BaseEntity)this.getValue()).getId() !=  null)
			return ((BaseEntity)this.getValue()).getId().toString();
		return super.getAsText();
	}

}
